package gqz.mvvm.view;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import gqz.mvvm.view.custom.SortView;

/**
 * Created by Administrator on 2018/6/11.
 */

public class SortActivity extends Activity {

    String TAG = "SortActivity->";

    SortView sortView;

    //    int[] arrsrc = {4, 2, 8, 9, 5, 7, 6, 1, 3};
    int[] arrsrc = {30, 20, 4, 31, 17, 10, 32, 23, 18, 33, 14, 28, 34, 8, 11, 35, 9, 21, 36, 1, 29, 37, 12,
            2, 38, 22, 15, 39, 13, 27, 16, 40, 19, 5, 24, 7, 6, 25, 3, 26};
    int[] arr;

    Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        sortView = new SortView(this);
        layout.addView(sortView);
        ViewGroup.LayoutParams params = sortView.getLayoutParams();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        sortView.setLayoutParams(params);
        setContentView(layout);

        int size = 200;
        HashSet<Integer> list = new HashSet<>();
//        Random random = new Random();
        for (int i = size; i > 0; i--) {
            list.add(i);
        }
        int index = 0;
        Iterator iterator = list.iterator();
        final int[] arrsrc = new int[list.size()];
        while (iterator.hasNext()) {
            arrsrc[index] = (int) iterator.next();
            index++;
        }
        while (iterator.hasNext()) {
            arrsrc[index] = (int) iterator.next();
            index++;
        }
        Log.i(TAG, "onCreate: arrsrc=" + Arrays.toString(arrsrc));
        arr = arrsrc.clone();
        sortView.setArray(arr);
        final Timer timer = new Timer();
        final int[] i = {1}, j = {0};
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (i[0] < arr.length) {

                            //bubble
                            /*for (int j = 0; j < arr.length - i[0] - 1; j++) {
                                if (arr[j] > arr[j + 1]) {
                                    int temp = arr[j + 1];
                                    arr[j + 1] = arr[j];
                                    arr[j] = temp;
                                    flag = false;
                                }
                            }
                            sortView.setArray(arr);
                            i[0]++;
                            */

                            //choice
                            /*int minIndex = i[0], tmp = 0;
                            for (int j = i[0]; j < arr.length; j++) {
                                tmp = j;
                                if (arr[j] < arr[minIndex]) minIndex = j;
                            }
                            if (minIndex != i[0]) {
                                int temp = arr[i[0]];
                                arr[i[0]] = arr[minIndex];
                                arr[minIndex] = temp;
                            }
                            sortView.setArray(arr);
                            i[0]++;*/

                            //insert
                            int tmp = arr[i[0]];
                            j[0] = i[0];
                            while (j[0] > 0 && arr[j[0] - 1] > tmp) {
                                arr[j[0]] = arr[j[0] - 1];
                                j[0]--;
                            }
                            arr[j[0]] = tmp;
                            sortView.setArray(arr);
                            i[0]++;


                            if (i[0] >= arr.length) {
                                arr = arrsrc.clone();
                                i[0] = 0;
                            }

                        }
                    }
                });
            }
        }, 1000, 50);
    }

    public void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            boolean flag = true;
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                    flag = false;
                }
            }
            if (flag) break;
            sortView.setArray(arr);
        }
    }
}
